<?php
/**
 * 详细
 * @author yulipu
 */
include LIB_PATH . 'Action/YAction.class.php';
class DetailAction extends YAction {
	public function __construct() {
		parent::__construct();
	}
	
	public function index() {
		$pre = C('DB_PREFIX');
		$id = y_getParam('id', 'get');
		if(!is_numeric($id)) {die('_param error');}
		
		$m = D('Article');
		$data = $m->getOneByPk($id);
		$this->assign('one', $data);
		
		// seo
		$selftitle = $data['title'];
		$selfdescription = $data['description'];
		$this->assign('selftitle', $selftitle);
		$this->assign('selfdescription', $selfdescription);
		
		// 更新浏览量
		$m->execute("update {$pre}article set viewnum=viewnum+1 where id={$id}");
		
		// 上一条 下一条
		$pre = M('Article')->field('id,title')->where("id < {$id}")->order('id desc')->limit(1)->find();
		$next = M('Article')->field('id,title')->where("id > {$id}")->order('id asc')->limit(1)->find();
		$this->assign('pre', $pre);
		$this->assign('next', $next);
		
		$this->display();
	}
	
	public function ajax_postComment() {
		$data = array();
		$data['pid'] = y_getParam('pid');  // 回复谁 如果不为 0 表示回复某个人
		$data['articleid'] = y_getParam('articleid');
		$data['nickname'] = y_getParam('nickname');
		$data['email'] = y_getParam('email');
		$data['website'] = y_getParam('website');
		$data['content'] = nl2br(y_getParam('content'));
		$data['notifyme'] = y_getParam('notifyme');  // 1 表示有人回复我时提示我
		
		if(!empty($data['email'])) {
			//$data['avatar'] = y_gravatar($data['email']);
		}
		
		$data['posttime'] = time();
		
		D('Comment')->addOne($data);
		
		$str = W('CommentSingle', array('dataList'=>$data));
		
		echo $str;
		
		// 处理回复
		if($data['pid'] != '0') {
			$this->_notify($data);
		}
	}
	
	private function _notify(& $data) {
		$one = D('Comment')->getOneByPk($data['pid'], 'articleid,nickname,email,notifyme');
		$url = y_getHost() . '/Detail/index/id/'.$one['articleid'];  // 文章链接
		$webname = SysUtil::loadOneConfigByKey('webname', './Public/seo_conf.php');
		
		if(!empty($one['email']) && $one['notifyme'] == '1') {
			$msg = '亲爱的 <b>'. $one['nickname'] .'</b> 您好。<br />'.
				"您在 {$webname} <a href=\"{$url}\">{$url}</a> 的留言有人回复了您，内容如下：<br />".
				$data['content'] . '<br /> 您也可以点击链接进行查看，若无法点击链接，您可以把链接复制到浏览器查看。<br />'.
				'若您从未在本站留言，则您的邮箱可能被冒用，请检查。';
				
			A('Mail')->sendMail($one['email'], '回复提示', $msg);
		}
	}
}